package com.scs.application.modules.device.enums;

import com.scs.application.core.exception.BusinessException;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述： 设备业务类型
 * 时间：2024年07月05日 14:03
 */
public class DeviceBusMap {
    private static Map<String,DeviceBus> busMap = new HashMap<>();
    // 转科
    public static final String DEVICE_TRANSFER_DEPT = "transferDept";
    // 报废
    public static final String DEVICE_SCRAP = "scrap";
    // 报修
    public static final String DEVICE_REPAIR = "repair";
    // 采购
    public static final String DEVICE_PO = "po";
    // 采购论证
    public static final String DEVICE_ARGUMENT = "po_argument";
    // 采购报价
    public static final String DEVICE_PO_OFFER = "po_offer";
    // 采购合同
    public static final String DEVICE_PO_CONTRACT = "po_contract";
    // 采购验收
    public static final String DEVICE_PO_ACCEPT = "po_accept";
    // 采购入库
    public static final String DEVICE_PO_WI = "po_wi";
    // 设备标签码
    public static final String DEVICE_SN = "device_sn";
    // 设备编码
    public static final String DEVICE_CODE = "device_code";
    // 租赁
    public static final String DEVICE_CIRCULATE_OUT = "circulate_out";
    // 租赁归还
    public static final String DEVICE_CIRCULATE_RETURN = "circulate_return";
    // 设备盘点
    public static final String DEVICE_CHECK = "check";
    // 设备调价
    public static final String DEVICE_PRICE_CHANGE = "price_change";
    // 设备折旧
    public static final String DEVICE_OLD = "old";

    // 保养计划
    public static final String plan_maintain = "device_plan_maintain";

    // 巡检计划
    public static final String plan_inspection = "device_plan_inspection";

    // 计量计划
    public static final String plan_metering = "device_plan_metering";

    // 保养任务
    public static final String device_task_maintain = "device_task_maintain";

    // 巡检任务
    public static final String device_task_inspection = "device_task_inspection";

    // 计量任务
    public static final String device_task_metering = "device_task_metering";

    @Data
    @Accessors(chain = true)
    public static class DeviceBus{
        // 业务类型
        private String busType;

        // 编号规则
        private String serialKey;

        // 流程结束时，需要将设备状态改为什么
        private String deviceStatusToIfEnd;

        // 该业务是否锁定了库存
        private Boolean flagLock = true;

        // 该业务是否需要更新台账信息
        private Boolean flagUpdateDeviceLedgerStatus = true;
    }

    static
    {
        // 转科
        DeviceBus deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_TRANSFER_DEPT).setSerialKey("device_transfer_dept");
        busMap.put(DEVICE_TRANSFER_DEPT, deviceBus);

        // 报废
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_SCRAP).setSerialKey("device_scrap").setDeviceStatusToIfEnd(DeviceStatus.scrapped);
        busMap.put(DEVICE_SCRAP, deviceBus);

        // 报修
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_REPAIR).setSerialKey("device_repair").setDeviceStatusToIfEnd(DeviceStatus.in_use);
        busMap.put(DEVICE_REPAIR, deviceBus);

        // 采购
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_PO).setSerialKey("device_po").setFlagUpdateDeviceLedgerStatus(false).setFlagLock(false);
        busMap.put(DEVICE_PO, deviceBus);

        // 采购论证
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_ARGUMENT).setSerialKey("device_po_argument").setFlagUpdateDeviceLedgerStatus(false).setFlagLock(false);
        busMap.put(DEVICE_ARGUMENT, deviceBus);

        // 采购报价
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_PO_OFFER).setSerialKey("device_po_offer").setFlagUpdateDeviceLedgerStatus(false).setFlagLock(false);
        busMap.put(DEVICE_PO_OFFER, deviceBus);

        // 采购合同
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_PO_CONTRACT).setSerialKey("device_po_contract").setFlagUpdateDeviceLedgerStatus(false).setFlagLock(false);
        busMap.put(DEVICE_PO_CONTRACT, deviceBus);

        // 采购验收
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_PO_ACCEPT).setSerialKey("device_po_accept").setFlagUpdateDeviceLedgerStatus(false).setFlagLock(false);
        busMap.put(DEVICE_PO_ACCEPT, deviceBus);

        // 采购入库
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_PO_WI).setSerialKey("device_po_wi").setFlagUpdateDeviceLedgerStatus(false).setFlagLock(false);
        busMap.put(DEVICE_PO_WI, deviceBus);

        // 租赁
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_CIRCULATE_OUT).setSerialKey("device_circulate_out").setDeviceStatusToIfEnd(DeviceStatus.in_use).setFlagLock(true);
        busMap.put(DEVICE_CIRCULATE_OUT, deviceBus);

        // 租赁归还
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_CIRCULATE_RETURN).setSerialKey("device_circulate_return").setDeviceStatusToIfEnd(DeviceStatus.free).setFlagLock(true);
        busMap.put(DEVICE_CIRCULATE_RETURN, deviceBus);

        // 设备盘点
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_CHECK).setSerialKey("device_check").setDeviceStatusToIfEnd(DeviceStatus.free).setFlagLock(false);
        busMap.put(DEVICE_CHECK, deviceBus);

        // 设备调价
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_PRICE_CHANGE).setSerialKey("device_check").setFlagUpdateDeviceLedgerStatus(false).setFlagLock(true);
        busMap.put(DEVICE_PRICE_CHANGE, deviceBus);

        // 设备折旧
        deviceBus = new DeviceBus();
        deviceBus.setBusType(DEVICE_OLD).setSerialKey("device_old").setFlagUpdateDeviceLedgerStatus(false).setFlagLock(false);
        busMap.put(DEVICE_OLD, deviceBus);

        // 保养计划
        deviceBus = new DeviceBus();
        deviceBus.setBusType(plan_maintain).setSerialKey(plan_maintain);
        busMap.put(plan_maintain, deviceBus);

        // 巡检计划
        deviceBus = new DeviceBus();
        deviceBus.setBusType(plan_inspection).setSerialKey(plan_inspection);
        busMap.put(plan_inspection, deviceBus);

        // 计量计划
        deviceBus = new DeviceBus();
        deviceBus.setBusType(plan_metering).setSerialKey(plan_metering);
        busMap.put(plan_metering, deviceBus);

        // 保养任务
        deviceBus = new DeviceBus();
        deviceBus.setBusType(device_task_maintain).setSerialKey(device_task_maintain);
        busMap.put(device_task_maintain, deviceBus);

        // 巡检任务
        deviceBus = new DeviceBus();
        deviceBus.setBusType(device_task_inspection).setSerialKey(device_task_inspection);
        busMap.put(device_task_inspection, deviceBus);

        // 计量任务
        deviceBus = new DeviceBus();
        deviceBus.setBusType(device_task_metering).setSerialKey(device_task_metering);
        busMap.put(device_task_metering, deviceBus);
    }

    // 根据业务类型，获取编号规则
    public  static String getSerialKey(String busType) {
        BusinessException.throwErrorIf(!busMap.containsKey(busType), "未找到业务类型 %s",busType);
        return busMap.get(busType).getSerialKey();
    }

    // 根据业务类型，获取配置
    public  static DeviceBus getDeviceBus(String busType) {
        BusinessException.throwErrorIf(!busMap.containsKey(busType), "未找到业务类型 %s",busType);
        return busMap.get(busType);
    }

}